package com.sf.config;

import com.sf.store.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class XiaozhiAgentConfig {

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Bean
    public ChatMemoryProvider chatMemoryProviderXiaozhi(){
        return memoryId-> MessageWindowChatMemory.builder().id(memoryId).maxMessages(20).chatMemoryStore(mongoChatMemoryStore).build();

    }


    @Bean
    ContentRetriever contentRetrieverXiaozhi() {
//使用FileSystemDocumentLoader读取指定目录下的知识库文档
//并使用默认的文档解析器对文档进行解析
        Document document1 = FileSystemDocumentLoader.loadDocument("E:\\微信\\硅谷小智（医疗版）\\资料\\knowledge\\易购乐电商平台服务体系文档.txt");
        Document document2 = FileSystemDocumentLoader.loadDocument("E:\\微信\\硅谷小智（医疗版）\\资料\\knowledge\\易购乐电商平台联系方式文档.md");
        Document document3 = FileSystemDocumentLoader.loadDocument("E:\\微信\\硅谷小智（医疗版）\\资料\\knowledge\\易购乐电商平台品类体系文档.txt");
        Document document4 = FileSystemDocumentLoader.loadDocument("E:\\微信\\硅谷小智（医疗版）\\资料\\knowledge\\易购乐电商平台运营保障文档.txt");
        List<Document> documents = Arrays.asList(document1, document2, document3,document4);
//使用内存向量存储
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
//使用默认的文档分割器
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
//从嵌入存储（EmbeddingStore）里检索和查询内容相关的信息
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }

}
